package cn.jobstart.common.express;

import cn.jobstart.common.HttpUtils;
import com.alibaba.fastjson.JSONObject;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sunke
 * @version 1.0
 * @Description 第三方快递查询
 * @date 2023-08-10 09:06
 */
public class ExpressUtils {


    public static void main(String[] args) {
        try {
            ExpressResult expressResult=getExpressInfo("770010457020498","","");

            System.out.println(expressResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 物流查询
     * @param number
     * @param mobile
     * @param expressCode
     * @return
     * @throws Exception
     */
    public static ExpressResult getExpressInfo(String number,String mobile,String expressCode) throws Exception{

        String host = "https://jmexpresv2.market.alicloudapi.com";
        String path = "/express/query-v2";
        String method = "POST";

        String appcode = "ce1365f7a8cc427398a69a1c53dd99ec";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("expressCode", expressCode);
        bodys.put("mobile", mobile);
        bodys.put("number", number);

        HttpResponse response = HttpUtils.doPostBody(host, path, method, headers, querys, bodys);
        String res=EntityUtils.toString(response.getEntity());
        System.out.println("getExpressInfo:"+res);
        return JSONObject.parseObject(res,ExpressResult.class);

    }




}
